let checkAllState = false

function loadCartList() {
    //获取localStorage数据
    let listStr = localStorage.getItem('Y-CART')
    let cartList = JSON.parse(listStr) || []

    let arr = ''
    let arr1 =''
    cartList.forEach(function (item) {

        let str = `
         <tr class="item" data-index="${item.id}" >
                    <td><input type="checkbox" class="xuan"  data-index="${item.id}" name="checksingle"  ${item.state?'checked':''}></td>
                    
                    <td> 
                        <img src="${item.url}" alt="">
                    </td>
                    <td>
                        <p>${item.product}</p>
                        <span>￥${item.price}</span>
                    </td>
                    <td class="item-i">
                    <input name="minus" type="button" value="-" ${item.num==0?'disabled':''}><input type="text" name="amount" value="${item.num}"><input
                    type="button" name="plus" value="+">
                    </td>
                    <td><a href="#" class="del">移除</a></td>
                </tr>      
         `
        let str1 = `
            <div><input type="checkbox" class="quan"  name="checkall" ${checkAllState?'checked':''}>&emsp;全选</div>
            <p>合计: <span class="total-price">￥${item.singlePrice}</span>元</p>
            <span class="sp">去结算</span>
`
        arr += str
        arr1= str1
    })
    $('.product-list>table').html(arr)
    $('.price').html(arr1)
    totalPrice()
    //全选栏



}

loadCartList()












function onCartList() {
    //加操作
    $('table').on('click', 'input[name="plus"]', function () {
        let cartStr = localStorage.getItem('Y-CART')
        let cartList = JSON.parse(cartStr) || []
        let id = Number($(this).parent().parent().attr('data-index'))

        let product = cartList.find(item => item.id == id)
        product.num++
        product.singlePrice = (product.num * product.price).toFixed(2) //计算价格

        //持久化存
        localStorage.setItem('Y-CART', JSON.stringify(cartList))

        //重新渲染页面
        loadCartList()

    })

    //减操作
    $('table').on('click', 'input[name="minus"]', function () {
        let cartStr = localStorage.getItem('Y-CART')
        let cartList = JSON.parse(cartStr) || []

        let id = $(this).parent().parent().attr('data-index')
        let product = cartList.find(item => item.id == id)
        product.num--

        product.singlePrice = (product.num * product.price).toFixed(2) //计算价格

        //持久化存
        localStorage.setItem('Y-CART', JSON.stringify(cartList))

        //重新渲染页面
        loadCartList()
    })
    //删除操作
    $('table').on('click', '.del', function () {
        let cartStr = localStorage.getItem('Y-CART')
        let cartList = JSON.parse(cartStr) || []
        let id = $(this).parent().parent().attr('data-index')
        //找索引号
        let index = cartList.findIndex(item => item.id == id)
        cartList.splice(index, 1)

        //持久化存
        localStorage.setItem('Y-CART', JSON.stringify(cartList))
        //重新渲染页面
        loadCartList()

    })

    //复选框
    $('table').on('click', 'input[name="checksingle"]', function () {
        let cartStr = localStorage.getItem('Y-CART')
        let cartList = JSON.parse(cartStr) || []

        let id = $(this).parent().parent().attr('data-index')

        //设置当前复选框数据
        let product = cartList.find(item => item.id == id)
        product.state = !product.state
        //全选框数据

        checkAllState = cartList.every(item => item.state == true)
        //持久化存
        localStorage.setItem('Y-CART', JSON.stringify(cartList))
        //重新渲染页面
        loadCartList()
    })
    //全选框
    $('.price').on('click', 'input[name="checkall"]', function () {
        let cartStr = localStorage.getItem('Y-CART')
        let cartList = JSON.parse(cartStr) || []
        //设置全选框状态
        checkAllState = !checkAllState
        //所有复选框状态
        cartList.forEach(item => item.state = checkAllState)
        //持久化存
        localStorage.setItem('Y-CART', JSON.stringify(cartList))
        //重新渲染页面
        loadCartList()
    })
}
/**
 * 计算选中商品总价
 *   将 state为true商品singlePrice价格累加
 */
function totalPrice() {
    let cartStr = localStorage.getItem('Y-CART')
    let cartList = JSON.parse(cartStr) || []

    let sum = 0
    cartList.forEach(item => {
        if (item.state) {
            sum += Number(item.singlePrice)
        }
    })
    $('.total-price').html(sum.toFixed(2))
}

//各界面跳转
$('footer>div').on('click', function () {
    $(this).addClass('active').siblings().removeClass('active')
})
$('.index1').on('click', function () {
    location.href = '../pages/index.html'
})
$('.mian1').on('click', function () {
    
    location.href = '../pages/mian.html'
})
$('.cart1').on('click', function () {
    location.href = '../pages/cart.html'
})
$('.login1').on('click', function () {
    location.href = '../pages/login.html'
})

onCartList()